<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>车辆模块增加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/js/jquery.js"></script>

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>用户角色信息维护</legend>
</fieldset>
<form class="layui-form" action="" lay-filter="carForm">

    <input type="hidden" name="carId" id="carId"><!-- 加一个隐藏主键 -->

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">车辆状态</label>
            <div class="layui-input-inline">
                <select name="carState" lay-verify="carState" id="carState">
                    <option value="">请选择</option>
                    <option value="保养中">保养中</option>
                    <option value="空闲中">空闲中</option>
                    <option value="使用中">使用中</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">车辆类型</label>
            <div class="layui-input-inline">
                <select name="carType" lay-verify="carType" id="carType">
                    <option value="">请选择</option>
                    <option value="小货车">小货车</option>
                    <option value="大货车">大货车</option>
                    <option value="面包车">面包车</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">车牌号</label>
            <div class="layui-input-inline">
                <input type="text" name="carNum" lay-verify="carNum|required" autocomplete="off" placeholder="请输入车牌号"
                       class="layui-input" id="carNum">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">所在地区</label>
            <div class="layui-input-inline">
                <input type="text" id="tree" name="driverArea" lay-filter="tree1" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item" id="staffName" >
        <div class="layui-block">
            <label class="layui-form-label" id="sj">司机</label>
            <div class="layui-input-inline">
                <input type="text" name="staffName" readonly autocomplete="off" class="layui-input" >
            </div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left: 200px;margin-top: 60px">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="tijiao">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" id="close_btn">关闭</button>
        </div>
    </div>
</form>

<script src="/res/layui/layui.js"></script>


<script>

    layui.config({
        base: "../authority/"//引入treeselect.js脚本
    }).extend({
        treeSelect: "/treeSelect"
    }).use(['form', 'treeSelect', 'jquery'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.$;
        var treeSelect = layui.treeSelect;

        $.ajaxSetup({
            async: false
        })

        let id = getQueryString("id");



        //自定义验证规则
        form.verify({
            userName: function (value) {
                if (value.length < 3) {
                    return '用户名至少得3个字符啊';
                }
            }
            ,
            pass: [
                /^[\S]{3,12}$/
                , '密码必须3到12位，且不能出现空格'
            ]
            ,
            ip: [/(^$)|((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))).){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))/,
                '请输入正确的ip地址'],
            carNum: [/(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$)/
                , '请输入正确的车牌号']
        })

        $(function () {
            $("#close_btn").on("click", function () {
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });

        //监听提交
        form.on('submit(tijiao)', function (data) {
            let data_ = data.field;
            data.field.carArea = a;
            let url = "/logisticsCar/addAndUpdate";
            $.post(url, data_, function (resp) {
                if (resp.state === 1) {
                    parent.table.reload('testReload');
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                } else {
                    layer.msg(resp.mgs);
                }
            }, "json");
            return false;
        });

        /**
         * 当点击编辑按钮时会传入一个id到form。html页面
         */
        if (id != null) {
            $.ajax({
                url: '/logisticsCar/show',
                dataType: 'json',
                data: {id: id},
                type: 'POST',
                success: function (resp) {
                    if (resp.state === 1) {
                        address = resp.data.carArea;
                        form.val('carForm', resp.data);
                        form.render();
                    }
                },
                error: function (response) {
                    console.log(response);
                }
            })
        } else {
            $("#staffName").html("");
            $("#carState").find("option[value="+"空闲中"+"]").attr("selected",'true');
            $("#carState").attr("disabled","disabled");
            form.render('select');
        }
        var a;
        var address;
        treeSelect.render({
            elem: '#tree',
            // 数据
            data: "/logisticsArea/selectYi",//json数据
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '请填写车辆所在地区',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            // 点击回调
            click: function (d) {
                a = d.current.id;
            },
            // 加载完成后的回调函数
            success: function (d) {
                //选中节点，根据id筛选
                treeSelect.checkNode('tree1', address);

                //  刷新树结构
                treeSelect.refresh();
            }
        })

    });

    /**
     * 得到地址上得值
     * @param id
     * @returns {string|null}
     */
    function getQueryString(id) {
        var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }


</script>
</body>
</html>